package com.example.smart_city_0829.activity.waimaidingcan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.waimai.CaiPing;
import com.example.smart_city_0829.entity.waimai.OrderList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class SearchResultActivity extends BaseActivity {

    private ListView lv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result2;
    }

    @Override
    protected void initView() {
        lv = (ListView) findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
        int type = getIntent().getIntExtra("type", -1);
        String key = getIntent().getStringExtra("key");

        switch (type){
            case 0:
                //店家
                api.getOrderList(key,null).enqueue(new NetCallBack<OrderList>() {
                    @Override
                    public void onResponse(Call<OrderList> call, Response<OrderList> response) {
                        if (response.isSuccessful()){
                            lv.setAdapter(new CommonAdapter<OrderList.RowsBean,CommonAdapter.CommListViewHolder>(
                                    response.body().getRows(),
                                    SearchResultActivity.this
                            ) {
                                @Override
                                protected void setData(OrderList.RowsBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
                                    Glide.with(context)
                                            .load(Constant.BASE_URL2 + data.getImgUrl())
                                            .into(viewHolder.im);
                                    viewHolder.title.setText(data.getName());
                                    viewHolder.content.setText(data.getIntroduction());
                                    viewHolder.tag2.setText("距离:"+data.getDistance());
                                    viewHolder.tag3.setText("地址:"+data.getAddress());
                                }

                                @Override
                                protected CommListViewHolder getViewHolder() {
                                    return new CommListViewHolder();
                                }

                                @Override
                                protected void initView(CommListViewHolder viewHolder, View convertView) {
                                    viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                                    viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                                    viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                                    viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                                    viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                                    viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
                                }
                            });
                        }
                    }
                });
                break;
            case 1:
                //菜品
                api.getCaiPing(key).enqueue(new NetCallBack<CaiPing>() {
                    @Override
                    public void onResponse(Call<CaiPing> call, Response<CaiPing> response) {
                        if (response.isSuccessful()){
                            List<CaiPing.RowsBean> rows = response.body().getRows();
                            ArrayList<CaiPing.RowsBean.ProductListBean> productListBeans = new ArrayList<>();
                            rows.forEach(data -> productListBeans.addAll(data.getProductList()));
                            lv.setAdapter(new CommonAdapter<CaiPing.RowsBean.ProductListBean,CommonAdapter.CommListViewHolder>(
                                    productListBeans,
                                    SearchResultActivity.this
                            ) {
                                @Override
                                protected void setData(CaiPing.RowsBean.ProductListBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
                                    Glide.with(context)
                                            .load(Constant.BASE_URL2 + data.getImgUrl())
                                            .into(viewHolder.im);
                                    viewHolder.title.setText(data.getName());
                                    viewHolder.tag3.setText("价格："+data.getPrice());
                                }

                                @Override
                                protected CommListViewHolder getViewHolder() {
                                    return new CommListViewHolder();
                                }

                                @Override
                                protected void initView(CommListViewHolder viewHolder, View convertView) {
                                    viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                                    viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                                    viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                                    viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                                    viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                                    viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
                                }
                            });
                        }
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected String getActionBarTitle() {
        return "搜索结果";
    }
}